package douyin

import (
	"GoServer/message"
	"GoServer/net/ws"
	"GoServer/utils/debug"
	"encoding/json"
)

type StartTaskMessage struct {
	message.Message

	Code     int
	TaskType string
	Data     string
}

func (msg *StartTaskMessage) MarshalBinary() ([]byte, error) {
	return json.Marshal(msg)
}

func OnStartTaskHandler(data []byte, gameName string) {
	msg := StartTaskMessage{}
	err := json.Unmarshal(data, &msg)
	if err != nil {
		debug.Logln(debug.Error, "OnStartTaskHandler, ", err)
		return
	}

	debug.Logln(debug.Debug, "启动抖音任务")

	msg.MsgType = message.SCStartTask

	result, err := StartLiveTask(msg.RoomId, msg.TaskType, gameName)
	if err == nil {
		msg.Data = string(result)
		b, _ := msg.MarshalBinary()
		ws.SendMessageToClient(msg.RoomId, b)
	} else {
		b, _ := msg.MarshalBinary()
		ws.SendMessageToClient(msg.RoomId, b)
	}
}

type CheckTaskMessage struct {
	message.Message

	TaskType string
}

/**检查任务当前状态*/
func OnCheckTaskHandler(data []byte, gameName string) {
	msg := StartTaskMessage{}
	err := json.Unmarshal(data, &msg)
	if err != nil {
		debug.Logln(debug.Error, "OnStartTaskHandler, ", err)
		return
	}

	debug.Logln(debug.Info, "检查抖音任务, ", msg.TaskType)

	_, commentError := CheckTaskStatus(msg.RoomId, "live_comment", gameName)
	if commentError != nil {
		/// 重新开启一次任务
		debug.Logln(debug.Info, "live_comment, 任务终止")
	}
	_, giftError := CheckTaskStatus(msg.RoomId, "live_gift", gameName)
	if giftError != nil {
		/// 重新开启一次任务
		debug.Logln(debug.Info, "live_gift, 任务终止")
	}
	_, likeError := CheckTaskStatus(msg.RoomId, "live_like", gameName)
	if likeError != nil {
		/// 重新开启一次任务
		debug.Logln(debug.Info, "live_like, 任务终止")
	}
}

//func OnSyncGameStatus(data []byte, gameName string) {
//	msg := models.SyncGameStatusData{}
//	err := json.Unmarshal(data, &msg)
//	if err != nil {
//		debug.Logln(debug.Error, "OnSyncGameStatus, 消息包体错误： ", err)
//		return
//	}
//
//	//mjxsql.SyncGameRoundStatus(&msg)
//}
